package goodmemo.client;

import java.util.ArrayList;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.ClientResponse;

import java.net.URI;
import javax.xml.bind.JAXBElement;
import javax.ws.rs.core.MediaType;

import goodmemo.server.MemoEntry;
import goodmemo.server.MemoEntries;


import javax.xml.namespace.QName ;
public class MemoClient {

    private static Client client=null;


    public MemoClient(){

        if (client == null)
            client = Client.create();
        

    }


    public boolean uploadMemo(String uri, MemoEntry me) {

        WebResource w = client.resource(uri);
        try{
            ClientResponse r=w.type(MediaType.APPLICATION_XML)
                    .post(ClientResponse.class,me);

            if (r.getStatus() != 200) {
                return false;
            }

        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public MemoEntries downloadMemo(String uri) {

        MemoEntries r = null;
        try {
            WebResource w = client.resource(uri);
            r = w.accept(MediaType.APPLICATION_XML_TYPE).get(MemoEntries.class);
        }catch(Exception e){
            e.printStackTrace();
        }

        return r;
    }


    
}
